package cn.tedu.charging.order.RabbitMQ;


import cn.tedu.charging.order.config.RabbitMqConfiguration;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RabbitMQConsumer {
    @Autowired
    private OrderRepository orderRepository;
    @RabbitListener(queues = RabbitMqConfiguration.DEAD_LETTER_QUEUE_NAME)

    public void receive(String orderNo, Message message, Channel channel) {
        System.out.println("消费到延迟消息:" + orderNo);
        log.debug("修改订单状态充电中的为异常结束");
        Integer row = orderRepository.updateOrderStatusFromChargingToAbnormalEnd(orderNo);
        if (row == 0){
            log.debug("没有更新数据，表示订单正常结束，设备没有出现故障");
        }else {
            log.debug("更新数据，表示订单异常结束，设备出现故障");
            log.debug("通知充电用户");//技术和业务的平衡
            log.debug("通知设备管理员");
        }

        //手动ack 消费者成功消费完消息后，业务处理成功，告诉RabbitMQ 消息已经消费完，可以删除消息了
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
